package com.learning.JavaSE.workExercise.module4.Task2;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;


public class FileABC2 {
    public static void main(String[] args) {
        ArrayList<String> inputList = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);

        System.out.println("开始输入字符串（输入'end'结束）：");
        String input = scanner.nextLine();
        while (!input.equals("end")) {
            inputList.add(input);
            input = scanner.nextLine();
        }
        scanner.close();
        Collections.sort(inputList); //按字典顺序排序
        Collections.reverse(inputList); //倒序排列
        writeToFile(inputList);
    }

    public static void writeToFile(ArrayList<String> inputList) {
        File file = new File("d:/PersonalCodeLearning/src/com/learning/lagou/JavaSE/workExercise/module4/Task2/abc.txt");
        try {
            FileWriter writer = new FileWriter(file);
            for (String s : inputList) {
                writer.write(s + "\n");
            }
            writer.close();
            System.out.println("成功将输入的字符串倒序录入到文件abc.txt中。");
        } catch (IOException e) {
            System.out.println("写入文件时发生错误：" + e.getMessage());
        }
    }
}

